<template>
  <div class="search" v-if="isQuery">
    <el-form
      :model="searchForm"
      ref="formRef"
      :label-width="searchConfig.labelWidth ?? '80px'"
      size="large"
      class="search-form"
    >
      <el-row :gutter="20">
        <template v-for="item in searchConfig.propItems" :key="item.prop">
          <el-col :span="item.elColSpan ?? 8">
            <el-form-item :label="item.label" :prop="item.prop">
              <template v-if="item.type === 'input'">
                <el-input
                  v-model="searchForm[item.prop!]"
                  :placeholder="item.placeholder"
                  :clearable="item.isClearable ?? false"
                />
              </template>
              <template v-else-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="searchForm[item.prop!]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  value-format="YYYY-MM-DD"
                  name="date-picker"
                />
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  v-model="searchForm[item.prop!]"
                  :placeholder="item.placeholder"
                  :clearable="item.isClearable ?? false"
                  style="width: 100%"
                >
                  <template v-for="option in item.options" :key="option.value">
                    <el-option :label="option.label" :value="option.value" />
                  </template>
                </el-select>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="wrap_btns">
      <div class="wrap">
        <div class="pencil"></div>
      </div>
      <div class="btns">
        <el-button icon="Refresh" @click="handleResetSearch">重置</el-button>
        <el-button icon="Search" type="primary" @click="handleQueryClick"
          >查询</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
/**
 * @author hzq
 * @date 2024/11/07
 * @description: 搜索组件
 * @status:done
 */

// 模块引入
import { ref, reactive } from 'vue'
import type { ElForm } from 'element-plus'
import type { ISearchConfig } from '@/components/types'
import { usePermissions } from '@/components/hooks'

defineOptions({
  // 组件名称
  name: 'PageSearchComp',
  // 继承透传，是否继承没在子组件声明的父组件的属性
  inheritAttrs: true
})

// 1、定义传入的属性接口
interface IProps {
  searchConfig: ISearchConfig
}

// 2、定义传入的属性
const props = defineProps<IProps>()
// 3、定义组件触发的事件
const emit = defineEmits(['queryClick', 'resetClick'])

const isQuery = usePermissions(`${props.searchConfig.pageName}:query`)
// 4、初始化表单
const initialForm: any = {}
for (const item of props.searchConfig.propItems) {
  if (item.prop) {
    initialForm[item.prop] = item.initialValue ?? ''
  }
}
const searchForm = reactive(initialForm)

// 5、表单重置
const formRef = ref<InstanceType<typeof ElForm>>()
function handleResetSearch() {
  if (!formRef.value) return
  formRef.value.resetFields()
  emit('resetClick')
}

// 6、查询
function handleQueryClick() {
  emit('queryClick', searchForm)
}
</script>

<style scoped lang="scss">
.search {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px 10px 0 0;
  position: relative;
  .search-form {
    // position: relative;
    // left: 0;
    // right: 0;
    width: 100%;
  }
  .el-form-item {
    padding: 20px 30px;
    margin-bottom: 0;
  }
}
.wrap_btns {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-right: 35px;
  // padding: 0 50px 10px 0;
  .btns {
    el-button {
      height: 36px;
    }
  }

  .wrap {
    opacity: 0.7;
    margin-left: 10px;
    transform-style: preserve-3d;
    .pencil {
      width: 80px;
      height: 20px;
      background:
        linear-gradient(to right, #e53466 70%, #d8d8d8 70%) no-repeat,
        linear-gradient(
          to bottom,
          #f06a35 33.33%,
          #f68c30 33.33%,
          #f68c30 66.66%,
          #ffa927 66.66%
        );
      background-size:
        30% 100%,
        70% 100%;
      border-radius: 8px 0 0 8px;
      position: relative;
      &::before,
      &::after {
        content: '';
        position: absolute;
        height: 20px;
      }
      &::before {
        width: 5px;
        right: 0;
        background: radial-gradient(
          circle at 5px 5px,
          #f4c09b 5px,
          transparent 0
        );
        background-size: 5px 12px;
      }
      &::after {
        width: 30px;
        background: linear-gradient(to right, #f4c09b 70%, #5e381f 70%);
        border-radius: 0 8px 8px 0;
        right: -30px;
        clip-path: polygon(0 0, 100% 50%, 0 100%);
      }
    }
  }
}
</style>
